package reviewAlgorithms.greedyThought;

import java.util.*;

public class EraseOverlapIntervals {
    public static void main(String[] args) {
        int[][] intervals = {{1,2},{2,3},{3,4},{1,3}};
        System.out.println(eraseOverlapIntervals(intervals));
    }

    public static int eraseOverlapIntervals(int[][] intervals) {
        if (intervals == null || intervals.length < 1){
            return 0;
        }
        int count = 0;
        Arrays.sort(intervals, Comparator.comparingInt(o -> o[0]));
        for (int i = 0; i < intervals.length - 1; i++) {
            if (intervals[i][1] > intervals[i + 1][0]){
                if (intervals[i][1] < intervals[i + 1][1]){
                    intervals[i + 1][1] = intervals[i][1];

                }else if (intervals[i][1] > intervals[i + 1][1]){
                    intervals[i][1] = intervals[i + 1][1];

                }
                count ++;
            }
        }

        return count;
    }
}
